#include "tipsbox.h"
#include "ui_tipsbox.h"

#include <QPainter>
#include <QDebug>
#include <QResizeEvent>
TipsBox::TipsBox(const QString &text, const MSG_TIP_TYPE &type, const quint16 &duration, QWidget *parent) :
    QWidget(parent),
    count(duration),
    m_nTipType(type),
    m_text(text),
    ui(new Ui::TipsBox)
{
    ui->setupUi(this);

    this->setMinimumSize(600,60);

    ShadowEffectInit();             //设置提示框的阴影效果

    // 初始化动画
    animation.setTargetObject(this);
    animation.setPropertyName("pos");
    animation.setDuration(800); // 动画持续时间为500毫秒

    animation.setStartValue(QPoint(1133 , getY));               // 开始位置在窗口上方
    animation.setEndValue(QPoint(1133 , getY + (getH/6)));       // 结束位置在窗口中央
//    animation.setStartValue(1333 ,  getY);               // 开始位置在窗口上方
//    animation.setEndValue(1333 , getY + (getH/6));       // 结束位置在窗口中央

    // 创建淡出动画
    fadeAnimation = new QPropertyAnimation(this, "windowOpacity");

    fadeAnimation->setDuration(300);    // 淡出动画持续时间
    fadeAnimation->setStartValue(1.0);  // 开始时完全不透明
    fadeAnimation->setEndValue(0.5);    // 结束时完全透明

    animation.setEasingCurve(QEasingCurve::InOutQuad);
    animation.start();

    // 设置窗体关闭时自动释放内存
    this->setAttribute(Qt::WA_DeleteOnClose);
}

TipsBox::~TipsBox()
{
    delete ui;
}

void TipsBox::ShadowEffectInit()
{
    time = new QTimer(this);
    connect(time,&QTimer::timeout,this,[=](){

        count--;
        qDebug()<<"duration"<<count;
        if(count <= 0)
        {
            // 当计数结束时，开始动画回退
            animation.setDirection(QAbstractAnimation::Backward);
            animation.start();

            QTimer::singleShot(200, this, [=](){
                fadeAnimation->start();

                connect(fadeAnimation,&QPropertyAnimation::finished,this,[=](){
                    this->deleteLater();
                });
            });
        }
    });

    time->start(1000);

    if(MSG_TIP_TYPE::SA_WARNING == m_nTipType){
        ui->icon->setStyleSheet("QLabel{"
                                "image: url(:/warning.png);"
                                "background:transparent;"
                                "}");
        ui->frame->setStyleSheet(".QFrame{"
                                 "background-color:rgb(254, 253, 236);"
                                 "border: 1px solid rgb(250, 236, 216);"
                                 "border-radius:8px;"
                                 "}");
        ui->text->setStyleSheet("QLabel{"
                                "font:32px;"
                                "color:rgb(231, 165, 65);"
                                "background:transparent;"
                                "Font-weight:bold;"
                                "}");
    }else if(MSG_TIP_TYPE::SA_FAILED == m_nTipType){
        ui->icon->setStyleSheet("QLabel{"
                                "image: url(:/error.png);"
                                "background:transparent;"
                                "}");
        ui->frame->setStyleSheet(".QFrame{"
                                 "background-color:rgb(254, 240, 240);"
                                 "border: 1px solid rgb(253, 226, 226);"
                                 "border-radius:8px;"
                                 "}");
        ui->text->setStyleSheet("QLabel{"
                                "font:32px;"
                                "color:rgb(245, 115, 123);"
                                "background:transparent;"
                                "Font-weight:bold;"
                                "}");
    }
    else if(MSG_TIP_TYPE::SA_SUCCESS == m_nTipType){
        ui->icon->setStyleSheet("QLabel{"
                                "image: url(:/success.png);"
                                "background:transparent;"
                                "}");
        ui->frame->setStyleSheet(".QFrame{"
                                 "background-color:rgb(240, 249, 235);"
                                 "border: 1px solid rgb(225, 243, 216);"
                                 "border-radius:8px;"
                                 "}");
        ui->text->setStyleSheet("QLabel{"
                                "font:32px;"
                                "color:rgb(108, 195, 66);"
                                "background:transparent;"
                                "Font-weight:bold;"
                                "}");
    }
    else if(MSG_TIP_TYPE::SA_TIPS == m_nTipType){
        ui->icon->setStyleSheet("QLabel{"
                                "image: url(:/tips.png);"
                                "background:transparent;"
                                "}");
        ui->frame->setStyleSheet(".QFrame{"
                                 "background-color:rgb(237, 242, 252);"
                                 "border: 1px solid rgb(237, 240, 246);"
                                 "border-radius:8px;"
                                 "}");
        ui->text->setStyleSheet("QLabel{"
                                "font:32px;"
                                "color:rgb(157, 160, 167);"
                                "background:transparent;"
                                "Font-weight:bold;"
                                "}");
    }

    ui->text->setText(m_text);

    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);

    // 在类的初始化或构造函数中添加
    QGraphicsDropShadowEffect *shadow = new QGraphicsDropShadowEffect(this);
    shadow->setOffset(0, 0);                // 阴影偏移量，可以设置为(0, 0)贴紧控件，或正负值产生偏移
    shadow->setColor(QColor(0, 0, 0,20)); // 阴影颜色及透明度
    //设置阴影圆角
    shadow->setBlurRadius(8);
    ui->frame->setGraphicsEffect(shadow);           //必须对frame设置阴影 否则报错
    //给垂直布局器设置边距(此步很重要, 设置宽度为阴影的宽度)
    this->setContentsMargins(2,2,2,2);
}
